package com.cloud.controller;

import com.cloud.domain.TUser;
import com.cloud.service.ITUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器 GUN
 * </p>
 *
 * @author jobob
 * @since 2021-06-23
 */
@RestController
@RequestMapping("/domain/t-user")
public class TUserController {
    @Autowired
    private ITUserService iTUserService;

    @RequestMapping("/getById")
    public TUser getById(@RequestBody Long id) {
        if (null == id) {
            return null;
        }
        return iTUserService.getById(id);
    }

    @RequestMapping("/save")
    public boolean save(@RequestBody TUser tUser) {
        if (null == tUser) {
            throw new RuntimeException("数据为空，插入失败");
        }
        return iTUserService.save(tUser);
    }

    @RequestMapping("/delete")
    public boolean delete(@RequestBody Long id) {
        if (null == id) {
          throw new RuntimeException("数据为空，删除失败");
        }
        return iTUserService.removeById(id);
    }

    @RequestMapping("/update")
    public boolean update(@RequestBody TUser tUser) {
        if (null == tUser) {
           throw new RuntimeException("数据为空，修改失败");
        }
        return iTUserService.updateById(tUser);
    }

    @RequestMapping("/listByPage")
    public PageInfo<TUser> listByPage(@RequestBody TUser tUser) {
        int pageNum = 1;
        int pageSize = 10;
        if (null != tUser && tUser.getPageNum() != null) {
            pageNum = tUser.getPageNum();
        }
        if (null != tUser && tUser.getPageSize() != null) {
           pageSize = tUser.getPageSize();
        }
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<TUser>(iTUserService.list());
    }

    @RequestMapping("/listAll")
    public List<TUser> listAll() {
        return iTUserService.list();
    }
}
